package com.woyou.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.woyou.bean.MyOrderItem;
import com.woyou.ui.activity.orderlist.BaseItemView;
import com.woyou.ui.activity.orderlist.DivisionItemView;
import com.woyou.ui.activity.orderlist.MultipleStatusItemView;
import com.woyou.ui.activity.orderlist.MultipleStatusItemView.ILoadOrderDetailListener;
import com.woyou.ui.activity.orderlist.WaitItemView;
import com.woyou.utils.LogUtil;

public class MyOrderAdapter extends BaseAdapter {
	private static final String TAG = "MyOrderAdapter";
	private List<MyOrderItem> mList;
	private Context mContext;
	
	private RefreshOrderListImp orderListImp;
	private ILoadOrderDetailListener listener;
	
	private static final int VIEW_TYPE = 3;
	private static final int TYPE_0 = 0; // 0是分隔线 ,1是订单,2是等待订单
	private static final int TYPE_1 = 1;
	private static final int TYPE_2 = 2;

	public MyOrderAdapter(List<MyOrderItem> mList, Context mContext, RefreshOrderListImp orderListImp, ILoadOrderDetailListener listener) {
		super();
		this.mList = mList;
		this.mContext = mContext;
		this.orderListImp = orderListImp;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 每个convertView都会调用此方法，获得当前所需要的view样式
	 */
	@Override
	public int getItemViewType(int position) {
		MyOrderItem item = mList.get(position);
		int type = item.Type;

		if (type == 0)
			return TYPE_0;
		else if (type == 1)
			return TYPE_1;
		else
			return TYPE_2;
	}

	@Override
	public int getViewTypeCount() {
		return VIEW_TYPE;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		MyOrderItem order = mList.get(position);
		int viewType = getItemViewType(position);
		LogUtil.d(TAG, convertView + "");

		if (convertView == null) {
			holder = new ViewHolder();
			switch (viewType) { // 按当前所需的样式，确定new的布局
			case TYPE_0: 
				// 分割线
				holder.view = new DivisionItemView(mContext);
				convertView = holder.view;
				convertView.setTag(holder);
				break;

			case TYPE_1: 
				// 订单
				holder.view = new MultipleStatusItemView(mContext, listener);
				convertView = holder.view;
				convertView.setTag(holder);
				break;

			case TYPE_2: 
				// 等待订单(即推送成功，state=2)
				holder.view = new WaitItemView(mContext);
				holder.view.setRefreshOrderListImp(orderListImp);
				convertView = holder.view;
				convertView.setTag(holder);
				break;
			default:
				break;
			}

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.view.loadData(order);
		return convertView;
	}

	public interface RefreshOrderListImp {
		/**
		 * 刷新订单列表
		 */
		void refreshOrder();
	}

	public class ViewHolder {
		BaseItemView view;
	}
	
	
}
